#define pr_fmt(fmt) "rtc: %s: " fmt, __func__

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/bcd.h>
#include <linux/rtc.h>
#include <linux/delay.h>
#include <linux/wakelock.h>
#include <linux/slab.h>
#include <linux/of_gpio.h>
#include <linux/irqdomain.h>



#define RTC_SECTION_LEN 0x01


#define pt7c43390_DEBUG
#ifdef pt7c43390_DEBUG
    #define debug_info(msg...) printk(msg);
#else
    #define debug_info(msg...)
#endif




struct pt7c43390 {
	int irq;
	struct i2c_client *client;
	struct mutex mutex;
};
static struct i2c_client *gClient;
struct pt7c43390 *pt7c43390;


static int i2c_master_reg8_send(const struct i2c_client *client,
		const char reg, const char *buf, int count)
{
	struct i2c_adapter *adap = client->adapter;
	struct i2c_msg msg;
	int ret;
	char *tx_buf = kzalloc(count , GFP_KERNEL);
	if (!tx_buf)
		return -ENOMEM;
	// tx_buf[0] = reg;
	memcpy(tx_buf, buf, count);

	msg.addr = client->addr;
	msg.flags = client->flags;
	msg.len = count ;
	msg.buf = (char *)tx_buf;

	//printk("send 0:0x%x,1:0x%x,2:0x%x,3:0x%x,4:0x%x,5:0x%x,6:0x%x,\r\n",tx_buf[0],tx_buf[1],tx_buf[2],tx_buf[3],tx_buf[4],tx_buf[5],tx_buf[6]);		       

	ret = i2c_transfer(adap, &msg, 1);
	kfree(tx_buf);
	return (ret == 1) ? count : ret;
}

static int i2c_master_reg8_recv(const struct i2c_client *client,
		const char reg, char *buf, int count)
{
	struct i2c_adapter *adap = client->adapter;
	struct i2c_msg msgs[2];
	int ret;
	char reg_buf = reg;

	msgs[0].addr = client->addr;
	msgs[0].flags = client->flags;
	msgs[0].len = 1;
	msgs[0].buf = &reg_buf;

	msgs[1].addr = client->addr;
	msgs[1].flags = client->flags | I2C_M_RD;
	msgs[1].len = count;
	msgs[1].buf = buf;

	ret = i2c_transfer(adap, msgs, 2);
	//printk("%s,i2c_transfer_ret=%d\n", __func__, ret);
	//printk("i2c_transfer ret:%d\r\n",ret);

	return (ret == 2) ? count : ret;
}

static int pt7c43390_i2c_read_regs(struct i2c_client *client,
		u8 reg, u8 buf[], unsigned len)
{
	int ret;
	ret = i2c_master_reg8_recv(client, reg, buf, len);
    //printk("%s,i2c_master_reg8_recv_ret=%d\n", __func__, ret);
	return ret;
}

static int pt7c43390_i2c_set_regs(struct i2c_client *client,
		u8 reg, u8 const buf[], __u16 len)
{
	int ret;
	ret = i2c_master_reg8_send(client, reg, buf, (int)len);
	return ret;
}




void rtc_state1_set(void){
	int ret;
	u8 reg;
	struct i2c_client *client= pt7c43390->client;
	ret=pt7c43390_i2c_read_regs(client, 0x30, &reg, 1);
	if(ret < 0){
		printk("i2c write error:%d\r\n",ret);
	}else{
		printk("i2c write ret:%d\r\n",ret);

	}
	reg=reg|(0x01<<6);
	pt7c43390_i2c_set_regs(client, 0x30, &reg,1);
}
EXPORT_SYMBOL(rtc_state1_set);

static int pt7c43390_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
		return -ENODEV;

	pt7c43390 = devm_kzalloc(&client->dev, sizeof(*pt7c43390), GFP_KERNEL);
	if (!pt7c43390)
		return -ENOMEM;

	gClient = client;
	pt7c43390->client = client;

	mutex_init(&pt7c43390->mutex);

	i2c_set_clientdata(client, pt7c43390);


	//




	return 0;
}

static int  pt7c43390_remove(struct i2c_client *client)
{
	// struct pt7c43390 *pt7c43390 = i2c_get_clientdata(client);
	 mutex_destroy(&pt7c43390->mutex);
	 devm_kfree(&client->dev,pt7c43390);
	return 0;
}


static const struct i2c_device_id pt7c43390_id[] = {
	{ "xagrtc_state",0},
	{ }
};
MODULE_DEVICE_TABLE(i2c, pt7c43390_id);

static struct of_device_id rtc_dt_ids[] = {
	{ .compatible = "xagrtc_state" },
	{},
};

struct i2c_driver pt7c43390_driver = {
	.driver     = {
		.name   = "xagrtc_state",
		.owner  = THIS_MODULE,
		.of_match_table = of_match_ptr(rtc_dt_ids),
	},
	.probe      = pt7c43390_probe,
	.remove     = pt7c43390_remove,
	.id_table   = pt7c43390_id,
};

static int __init pt7c43390_init(void)
{
	printk("%s,%d\r\n",__FUNCTION__,__LINE__);
	return i2c_add_driver(&pt7c43390_driver);
}

static void __exit pt7c43390_exit(void)
{
	printk("%s,%d\r\n",__FUNCTION__,__LINE__);
	i2c_del_driver(&pt7c43390_driver);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xag");

module_init(pt7c43390_init);
module_exit(pt7c43390_exit);
